<?php

namespace App\Model;

use App\Http\Controllers\Constant\Constant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;

/**
 * @property integer $xmid
 * @property integer $uid
 * @property integer $create_role
 * @property integer $work_id
 * @property integer $node_id
 * @property integer $roleid
 * @property string $money
 * @property integer $paytype
 * @property integer $status
 * @property string $created_at
 * @property string $updated_at
 * @property string $title
 * @property string $approver
 * @property integer $is_save
 * @property string $desc
 * @property string $role_list
 * @property integer $ticket_id
 * @property integer $ticket_category
 * @property integer $ticket_type
 * @property string $batch_id
 * @property string $username
 * @property  string $type_name
 * @property array $summary
 * @property integer $ysid
 * @property integer $deptid
 */
class OAFinance extends OABaseModel
{
    protected $table = 'finance';
    protected $appends = ['type_name','username','cw_title'];
    protected $hidden = ['approver','role_list','staff'];
    public $timestamps = true;
    protected $fillable = [
       'xmid','uid','create_role','work_id',
       'node_id','roleid','money','paytype',
       'status','created_at','updated_at','title',
       'approver','is_save','desc','role_list',
       'ticket_id','ticket_category','ticket_type',
        'batch_id','ysid','deptid'
    ];
    protected $attributes = ['approver' => 'array','role_list'=>"array"];
    protected $casts = ['approver' => 'array','role_list'=>"array"];

    public static function delCwData($params)
    {
        if(array_key_exists('xmid',$params)){
            //查差旅
            $clv = self::query()->where('xmid',$params['xmid'])->where('paytype',1)->first();
            if(!is_null($clv)) self::delClv($clv->id);
            self::query()->where('xmid',$params['xmid'])->delete();
        }else{
            $finance = self::query()->where('id',$params['id'])->first();
            if($finance->paytype == 1) self::delClv($finance->id);
            self::query()->where('id',$params['id'])->delete();
        }
    }

    public static function delClv($clvid)
    {
        return OAFinanceClv::query()->where('pid',$clvid)->delete();
    }

    public function lx()
    {
        return $this->belongsTo(OAProjectLx::class,'xmid');
    }

    public function clvs()
    {
        return $this->hasMany(OAFinanceClv::class,'pid');
    }

    //获取已申请金额
    public function getSummaryAttribute()
    {
        //限额
        $ysDetail = OAProjectYsDetail::query()->find($this->ysid);
        if($ysDetail){
            $limit = bcadd($ysDetail->limitMoney,'0.00',2);
        }else{
            $cwTitle = $this->paytype == Constant::PAY_TYPE_FK ? $this->title : '';
            $limit = OAProjectYsDetail::getLimitMoney($this->xmid,$this->paytype,$cwTitle);
        }

        //已申请
        $yi = self::query()
            ->where('ysid',$this->ysid)
            ->where('paytype',$this->paytype)
            ->sum('money');
        $yi = bcadd($yi,'0.00',2);
        $wei = bcsub($limit,$yi,2);
        $limit = number_format($limit,2);
        $yi = number_format($yi,2);
        $wei = number_format($wei,2);
        return compact('limit','yi','wei');
    }

    public function getTypeNameAttribute()
    {
        return Constant::getFinanceCategory($this->paytype);
    }

    public function staff()
    {
        return $this->belongsTo(OAStaff::class,'uid');
    }

    public function getUsernameAttribute()
    {
        return optional($this->staff)->username ?? '-';
    }

    public function getCwTitleAttribute()
    {
        return $this->title;
    }

}
